package com.sctvcloud.renhe.ui.activities;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.beans.NewsItem;
import com.sctvcloud.renhe.http.AbsListNetCallback;
import com.sctvcloud.renhe.http.NetUtils;
import com.sctvcloud.renhe.ui.adapter.VideoListAdapter;
import com.sctvcloud.renhe.ui.util.RBaseItemDecoration;
import com.sctvcloud.renhe.ui.utils.SkipUtil;
import com.sctvcloud.renhe.ui.utils.TitleUtils;
import com.sctvcloud.renhe.utils.ListTypeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门视频更多的列表页
 */
public class VideoActivity extends BaseActivity implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {
    @BindView(R.id.video_refreshLayout)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView video_list;
    private LinearLayoutManager manger;
    private VideoListAdapter adapter;
    private String videoUrl;
    private int currentPage = 0;
    private int pageAll = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_video);
        ButterKnife.bind(this);
    }

    private void initView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        video_list.addItemDecoration(new RBaseItemDecoration(2));
        manger = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        video_list.setItemAnimator(new DefaultItemAnimator());
        video_list.setLayoutManager(manger);
        videoUrl = getIntent().getStringExtra(EX_HTML);
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadMore() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.loadMoreComplete();
        } else {
            currentPage = currentPage + 1;
            if (currentPage < pageAll) {
                getHotVideoList(currentPage);
            }
        }

    }

    @Override
    public void onRefresh() {
        currentPage = 0;
        getHotVideoList(currentPage);
    }

    private void getHotVideoList(final int calledPage) {
        if (TextUtils.isEmpty(videoUrl)) {
            toast("数据错误");
            return;
        }
        NetUtils.getNetAdapter().getNewsPage(getOwnerName(), videoUrl, calledPage, new AbsListNetCallback<NewsItem>(ListTypeUtils.NewsItem(), calledPage) {
            @Override
            public void onSuc(List<NewsItem> newsItems) {
                if (ListUtils.isListValued(newsItems)) {
                    pageAll = getPageResponse().getData().getPageAll();
                    currentPage = getPageResponse().getData().getPageIndex();
                    setNewsData(newsItems);
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.loadMoreComplete();
        }
        refreshLayout.setLoadMoreEnabled(pageAll > currentPage + 1);
    }

    private void setNewsData(List<NewsItem> newsItem) {
        if (currentPage == 0) {
            if (adapter == null) {
                adapter = new VideoListAdapter(this, newsItem).setItemInternalClick(onItemInternalClick);
                video_list.setAdapter(adapter);
            } else {
                adapter.setData(newsItem);
            }
        } else {
            adapter.addDatas(newsItem);
        }
    }

    OnItemInternalClick onItemInternalClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            NewsItem newsItem = null;
            if (view == null) {
                newsItem = (NewsItem) itemView.getTag(R.id.adapter_item_data_key);
            } else {
                newsItem = (NewsItem) itemView.getTag(R.id.adapter_item_data_key);
            }
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "首页"));
            pairs.add(new Pair<>("栏目", "精选"));
            pairs.add(new Pair<>("位置", "热门视频_更多"));
            HashMap<String, String> map = new HashMap<>();
            for (Pair<String, String> p : pairs) {
                map.put(p.first, p.second);
            }
            if (newsItem != null) {
                SkipUtil.skipWithNewsItem(VideoActivity.this, newsItem, map);
            } else {
                toast("数据错误");
            }

        }
    };

}
